<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Menu extends Admin_Controller
{
    public function index()
    {
        $rule = $this->authRule_m->get_rules_available();
        $authRule = $this->getMenu($rule);
        $data['authRule'] = $authRule;
        $this->load->view('menu/index', $data);
    }

    public function del()
    {
        $post = $this->input->post();
        $ids = isset($post['ids']) ? $post['ids'] : false;
        if (!$ids) {
            show_message('请勾选删除菜单',site_url(X_URL.'menu/index'),1);
        }
        //uid为1的禁止删除
        if (is_array($ids)) {
            foreach ($ids as $k => $v) {
                $ids[$k] = intval($v);
            }
            if (!$ids) {
                show_message('参数错误',site_url(X_URL.'menu/index'),1);
            }
        }

        $map['id'] = array('in', $ids);
        $bool = $this->authRule_m->delete_menu_by_where($map);
        if ($bool) {
            show_message('恭喜，菜单删除成功',site_url(X_URL.'menu/index'),1);
        } else {
            show_message('参数错误',site_url(X_URL.'menu/index'),1);
        }
    }

    public function edit($id = 0)
    {

        if(!empty($id) && is_numeric($id)){


            $rule = $this->authRule_m->get_rules_by_id($id);

            $option = $this->authRule_m->get_rules_available();

            $option = $this->getMenu($option);
            $data['option'] = $option;
            $data['currentmenu'] = $rule;
            $this->load->view('menu/form', $data);
        }else{
            echo "参数缺失";exit();
        }
    }

    public function update()
    {
        $post = $this->input->post ();
        $id = isset($post['id']) ? intval($post['id']) : false;
        $data['pid'] = isset($post['pid']) ? $post['pid'] : "";
        $data['title'] = isset($post['title']) ? $post['title'] : "";
        $data['name'] = isset($post['name']) ? trim($post['name']) : "";
        $data['icon'] = isset($post['icon']) ? $post['icon'] : "";
        $data['islink'] = isset($post['islink']) ? $post['islink'] : "";
        $data['status'] = 1;
        $data['o'] = isset($post['o']) ? intval($post['o']) : false;
        $data['tips'] = isset($post['tips']) ? intval($post['tips']) : false;
        if ($id) {
            $rules = $this->authRule_m->sava_menu_rule($id,$data);
        } else {
            $rules = $this->authRule_m->add_menu_rule($data);
        }
        if ($rules) {
            show_message('操作成功',site_url(X_URL.'menu/index'),1);
        } else {
            show_message('操作失败',site_url(X_URL.'menu/edit/'.$id),1);
        }
    }


    public function add()
    {

        $option = $this->authRule_m->get_rules_available();

        $option = $this->getMenu($option);
        $data['option'] = $option;
        $this->load->view('menu/form', $data);
    }
}